SELECT deptno, empno, sal FROM emp e WHERE 2 > ( SELECT COUNT(e1.sal) FROM emp e1 WHERE e.deptno = e1.deptno AND e.sal < e1.sal ) ORDER BY 1,3 DESC; SELECT * FROM EMP A WHERE &N > ( SELECT COUNT(DISTINCT SAL) FROM EMP B WHERE B.SAL>A.SAL) ORDER BY A.SAL DESC; SELECT * FROM (SELECT * FROM emp ORDER BY salary DESC) WHERE ROWNUM <3
I am looking at the following output. We need to stick to this format.
SELECT COUNT (*), COUNT(DECODE(TO_CHAR (hiredate, 'YYYY'),'1980', empno)) "1980", COUNT (DECODE (TO_CHAR (hiredate, 'YYYY'), '1981', empno)) "1981", COUNT (DECODE (TO_CHAR (hiredate, 'YYYY'), '1982', empno)) "1982", COUNT (DECODE (TO_CHAR (hiredate, 'YYYY'), '1983', empno)) "1983" FROM emp;
SELECT a.deptno, ename, sal, (SELECT SUM(sal) FROM emp b WHERE a.deptno =b.deptno) FROM emp a ORDER BY a.deptno;
OUTPUT :
======= | ||||
DEPTNO | ENAME | SAL | SUM (SAL) | |
========= | ======= | ==== | ========= | |
10 | KING | 5000 | 11725 | |
30 | BLAKE | 2850 | 10900 | |
10 | CLARK | 2450 | 11725 | |
10 | JONES | 2975 | 11725 | |
30 | MARTIN | 1250 | 10900 | |
30 | ALLEN | 1600 | 10900 | |
30 | TURNER | 1500 | 10900 | |
30 | JAMES | 950 | 10900 | |
30 | WARD | 2750 | 10900 |
20 | SMITH | 8000 | 33000 |
20 | SCOTT | 3000 | 33000 |
20 | MILLER | 20000 | 33000 |
Inclined to build a profession as MicroStrategy Developer? Then here is the blog post on, explore MicroStrategy Training
The output is as follows - we need to stick to this format :
Job | Dept 10 | Dept 20 | Dept 30 | |
Total | ||||
---------- | --------------- | ------------- | ------------- | - |
-------- | ||||
ANALYST | 6000 | |||
6000 | ||||
CLERK | 1300 | 1900 | 950 | |
4150 | ||||
MANAGER | 2450 | 2975 | 2850 | |
8275 | ||||
PRESIDENT | 5000 |
5000 | ||||
SALESMAN | 5600 | |||
5600 |
SELECT job "Job", SUM (DECODE (deptno, 10, sal)) "Dept 10", SUM (DECODE (deptno, 20, sal)) "Dept 20", SUM (DECODE (deptno, 30, sal)) "Dept 30", SUM (sal) "Total" FROM emp GROUP BY job ;
SELECT DEPTNO, ENAME, SAL FROM EMP A WHERE 3 = (SELECT COUNT(B.SAL) FROM EMP B WHERE A.SAL < B.SAL) ORDER BY SAL DESC; SELECT * FROM EMP A WHERE &N-1 = (SELECT COUNT (DISTINCT SAL) FROM EMP B WHERE B.SAL>A.SAL); Alternate SELECT ename, deptno, sal ROM (SELECT * FROM emp ORDER BY sal DESC) WHERE ROWNUM < N;
ELECT DEPTNO, ENAME, SAL FROM EMP WHERE ROWID = (SELECT ROWID FROM EMP WHERE ROWNUM <= 5 MINUS SELECT ROWID FROM EMP WHERE ROWNUM < 5
select rownum,empno,ename from emp group by rownum,empno,ename having mod(rownum,2)=0;
OR
select * from emp where rowid in (select decode(mod(rownum,2),0,rowid) from emp)
OR
For Even Rownumber Select * from emp Where (rowid,0) in (select rowid, mod(rownum,2) from emp) For Odd Rownumber Select * from emp Where (rowid, 1) in (select rowid, mod(rownum,2) from emp)
Name Null? Type ------------------------------------------------------------------- SUB NOT NULL VARCHAR2(4) SUPER VARCHAR2(4) PRICE NUMBER(6,2) SELECT sub, super FROM parts CONNECT BY PRIOR sub = super START WITH sub = 'p1';
DELETE FROM table_name A WHERE ROWID > ( SELECT min(ROWID) FROM table_name B WHERE A.col = B.col); OR DELETE FROM table_name A WHERE ROWID < ( SELECT max(ROWID) FROM table_name B WHERE A.col = B.col);
SELECT *FROM emp WHERE (ROWID,0) IN (SELECT ROWID, MOD(ROWNUM,4) FROM emp); ALTERNATE SELECT * FROM TB_CNR014_DIM_BRAND WHERE ROWID IN (SELECT DECODE(MOD(BRAND_ID, 4), 0, ROWID) FROM TB_CNR014_DIM_BRAND)
MicroStrataegy Advanced Interview Questions
SELECT ename, deptno, sal FROM (SELECT * FROM emp ORDER BY sal DESC) WHERE ROWNUM < 10;
COUNT(DECODE(greatest(f1,59), least(f1,100), 1, 0)) "Range 60-100", COUNT(DECODE(greatest(f1,30), least(f1, 59), 1, 0)) "Range 30-59", COUNT(DECODE(greatest(f1,29), least(f1, 0), 1, 0)) "Range 00-29" FROM my_table GROUP BY f2; Correct Answer: Do not Consider 0 SELECT COUNT(*), count(decode( greatest(field_3,20),least(field_3,49),1)) AS "Range 20-50", count(decode( greatest(field_3,50),least(field_3,59),1)) AS "Range 50-60", count(decode( greatest(field_3,60),least(field_3,69),1)) AS "Range 60-70", count(decode( greatest(field_3,90),least(field_3,99),1)) AS "Range 90-100" FROM test_mapping
SELECT ename "Name", sal "Salary", DECODE( TRUNC(sal/1000, 0), 0, 0.0, 1, 0.1, 2, 0.2, 3, 0.3) "Tax rate" FROM emp;
COL NAME DATATYPE ---------------------------------------- DNO NUMBER SEX CHAR SELECT dno, SUM(DECODE(sex,'M',1,0)) MALE, SUM(DECODE(sex,'F',1,0)) FEMALE, COUNT(DECODE(sex,'M',1,'F',1)) TOTAL FROM t1 GROUP BY dno;
SELECT EXP(SUM(LN(col1))) FROM srinu;
SELECT num FROM satyam GROUP BY num HAVING COUNT(*) > 1;
* ** *** **** ***** SELECT RPAD(DECODE(temp,temp,'*'),ROWNUM,'*') FROM srinu1;
select next_day(last_day(sysdate)-7,’thursday’) from dual;
Update emp set sal= Case job When ‘CLERK’ then sal+500 When ‘SALESMAN’ then sal+600 When ‘MANAGER’ then sal+2000 Else sal End; OR Update emp set sal= Decode(job,’CLERK’,sal+200,’SALESMAN’,sal+300,’MANAGER’,sal+500);
SELECT ENAME,TRUNC(MONTHS_BETWEEN(SYSDATE,HIREDATE)/12) YEAR, MOD(TRUNC(MONTHS_BETWEEN(SYSDATE,HIREDATE)),12) MONTH FROM EMP
DNO | ASAL | ECOUNT | ENAME | SAL JOB | |||
---- ---------- ---------- | ---------- ---------- | --------- | |||||
10 | 2916.66667 | 3 CLARK 2450 MANAGER |
KING | 5000 PRESIDENT | ||||||
MILLER | 1300 CLERK | ||||||
3614.8 | 5 SMITH | 4000 CLERK | |||||
ADAMS | 1100 CLERK | ||||||
FORD | 3000 ANALYST | ||||||
SCOTT | 6999 ANALYST | ||||||
JONES | 2975 MANAGER | ||||||
1400 | 6 ALLEN | 600 SALESMAN | |||||
BLAKE | 2850 MANAGER | ||||||
MARTIN | 1250 SALESMAN | ||||||
JAMES | 950 CLERK | ||||||
TURNER | 1500 SALESMAN | ||||||
WARD | 1250 SALESMAN |
SOLUTION:
BREAK ON DNO ON ASAL ON ECOUNT SKIP 1 select d.deptno dno,avg(d.sal) asal,count(d.ename) ecount, e.ename,e.sal,e.job from emp d,emp e where d.deptno=e.deptno group by d.deptno,e.ename,e.sal,e.job
OR
SELECT DNO,ASAL,ECOUNT,E.ENAME,E.SAL,E.JOB FROM (SELECT DEPTNO DNO, AVG(SAL) ASAL,COUNT(ENAME) ECOUNT FROM EMP GROUP BY DEPTNO),EMP E WHERE DNO=E.DEPTNO;
select deptno,max(sal) from emp group by deptno union
select deptno,max(sal) from (select deptno,sal from emp minus select deptno,max(sal) from emp group by deptno) group by deptno
or
select sal from emp e1 where &n>=(select count(distinct sal) from emp e2 where e1.sal<=e2.sal and e1.deptno=e2.deptno)
select sysdate from dual;
Select to_char(sysdate+3/24,’hh’) from dual;
Select to_char(sysdate+1/48,’hh:mi’) from dual;
Select to_char(sysdate+10/1440,’mi’) from dual;
Select to_char(sysdate+10/86400,’SS’) from dual;
Select to_char(trunc(sysdate+1),’dd-mm-yy-/hh:mi:ss’) from dual;
Select to_char(trunc(sysdate+1)+8/24) from dual;
Select next_day(trunc(sysdate),'Monday')+12/24 from dual;
Select trunc(last_day(sysdate)+1) from dual;
Select last_day(add_months(sysdate,-1))+1 from dual;
Select vsize(ename) from emp;
Select to_date(sysdate)-to_date('14-Aug-1980') Days from dual;
select trunc(trunc(months_between(sysdate,'14-aug-80'))/12) year from dual;
select e.ename from emp e,emp m where e.mgr=m.empno and e.sal>m.sal;
select e.ename from emp e,emp m where e.mgr=m.empno and e.deptno=m.deptno;
select e.ename from emp e,emp m where e.mgr=m.empno and m.ename=’BLAKE’;
Select next_day(sysdate,’saturday’) from dual; DDL Queries.
alter table ddl rename column empno to eno;
rename ddl to emp;
alter table ttl drop column sal
Alter table ttl add(sal number(4));
Alter table ttl add(empno number primary key);
select count(*) from cols where table_name='EMP';
Insert into emp1 select * from emp;
Create table emp1 as select * from emp;
create index i1 on ttl(empno); create bitmap index bi on ttl(mgr); create index i2 on ttl(job) online;
Create cluster c1 (empno number);
create or replace trigger multi_table_update after update of sal on emp for each row begin update emp1 set sal=:new.sal where empno=7369; update emp2 set sal=:new.sal where empno=7369; end; / DROP SEQUENCE S1; CREATE SEQUENCE S1 START WITH 1 INCREMENT BY 1 MAXVALUE 15000 MINVALUE 1 NOCACHE NOCYCLE; EXEC PROCEDURE_SCD2; SELECT * FROM PROC_SCD2;
DELETE FROM srinu WHERE (ROWID,0) IN (SELECT ROWID, MOD(ROWNUM,2) FROM srinu);
DELETE FROM srinu WHERE (ROWID,1) IN (SELECT ROWID, MOD(ROWNUM,2) FROM srinu);
SELECT ename, NVL(LENGTH(REPLACE(TRANSLATE(UPPER(RTRIM(ename)),'ABCDEFGHIJKLMNOPQRS TUVWXYZ'' ',' @'),' ',''))+1,1) word_length FROM emp; Explanation : TRANSLATE(UPPER(RTRIM(ename)),'ABCDEFGHIJKLMNOPQRSTUVWXYZ'' ',' @') -- This will translate all the characters FROM A-Z including a single quote to aspace. It will also translate a space to a @.
REPLACE(TRANSLATE(UPPER(RTRIM(ename)),'ABCDEFGHIJKLMNOPQRSTUVWXYZ'' ','@'),' ','') -- This will replace every space with nothing in the above result.
LENGTH(REPLACE(TRANSLATE(UPPER(RTRIM(ename)),'ABCDEFGHIJKLMNOPQRSTUVWXYZ'' ',' @'),' ',''))+1 --
This will give u the count of @ characters in the above result.
Alternate:
SELECT length('samir') - length(REPLACE('samir','r','')) FROM dual;
CREATE OR REPLACE FUNCTION is_leap_year (p_date IN DATE) RETURN VARCHAR2 AS v_test DATE; BEGIN v_test := TO_DATE ('29-Feb-' || TO_CHAR (p_date,'YYYY'),'DD-Mon-YYYY'); RETURN 'Y'; EXCEPTION WHEN OTHERS THEN RETURN 'N'; END is_leap_year; SQL> SELECT hiredate, TO_CHAR (hiredate, 'Day') weekday FROM emp WHERE is_leap_year (hiredate) = 'Y';
SELECT TRANSLATE(LOWER(ssn),'abcdefghijklmnopqrstuvwxyz- ','') FROM DUAL;
SELECT TRANSLATE(INITCAP(temp), SUBSTR(temp, INSTR(temp,'''')+1,1), LOWER(SUBSTR(temp, INSTR(temp,'''')+1))) FROM srinu1;
SELECT TO_CHAR( TO_DATE( SUBSTR( TO_CHAR(5373484),1),'j'),'Jsp') FROM DUAL;
Only up to integers from 1 to 5373484
SELECT case WHEN sex = 'm' THEN 'male' WHEN sex = 'f' THEN 'female' ELSE 'unknown' END FROM mytable
CREATE TABLE srinu(dt1 date DEFAULT SYSDATE, dt2 date, CONSTRAINT check_dt2 CHECK ((dt2 >= dt1) AND (dt2 <= ADD_MONTHS(SYSDATE,3)));
SELECT DISTINCT a.SUPP FROM ORDERS a WHERE a.supp != 'S2' AND a.parts IN (SELECT DISTINCT PARTS FROM ORDERS WHERE supp = 'S2') GROUP BY a.SUPP HAVING COUNT(DISTINCT a.PARTS) >= (SELECT COUNT(DISTINCT PARTS) FROM ORDERS WHERE supp = 'S2');
Table: orders SUPP PARTS ------------------ -------
S1 | P1 |
S1 | P2 |
S1 | P3 |
S1 | P4 |
S1 | P5 |
S1 | P6 |
S2 | P1 |
S2 | P2 |
S3 | P2 |
S4 | P2 |
S4 | P4 |
S4 | P5 |
SELECT NEXT_DAY(LAST_DAY(TO_DATE('26-10-2001','DD-MM-YYYY')) - 7,'sunday') FROM DUAL; SELECT NEXT_DAY(to_date(LAST DAY OF THE MONTH)-7, 'sunday') FROM dual
table data: id
name parent_id
-------------------------------
a NULL - the top-level entry
b 1 - a child of 1
3 | c | 1 |
4 | d | 2 - a child of 2 |
5 | e | 2 |
6 | f | 3 |
7 | g | 3 |
8 | h | 4 |
9 | i | 8 |
10 j 9
SELECT ID FROM MY_TABlE WHERE PARENT_ID IS NOT NULL MINUS SELECT PARENT_ID FROM MY_TABlE;
SELECT empno FROM emp WHERE ROWID in (SELECT ROWID FROM emp MINUS SELECT ROWID FROM emp WHERE ROWNUM <= (SELECT COUNT(*)-5 FROM emp)); SELECT * FROM test_mapping WHERE ROWNUM < 11 MINUS SELECT * FROM test_mapping WHERE ROWNUM < 9
CREATE OR REPLACE PROCEDURE disp AS xTableName varchar2(25):='emp'; xFieldName varchar2(25):='ename'; xValue NUMBER; xQuery varchar2(100); name varchar2(10) := 'CLARK'; BEGIN xQuery := 'SELECT SAL FROM ' || xTableName || ' WHERE ' || xFieldName || ' = ''' || name || ''''; DBMS_OUTPUT.PUT_LINE(xQuery); EXECUTE IMMEDIATE xQuery INTO xValue; DBMS_OUTPUT.PUT_LINE(xValue); END;
SELECT name FROM v$database;
SELECT SYS_CONTEXT('USERENV','CURRENT_SCHEMA') FROM DUAL;
SELECT column_name FROM all_tab_columns WHERE TABLE_NAME = 'ORDERS';
Place the following lines of code in a file and execute the file in SQLPLUS :
set heading off set feedback off set colsep ' ' set termout off set verify off spool c:srini.txt SELECT empno,ename FROM emp; /* Write your Query here */ spool off /
SELECT SYS_CONTEXT('USERENV','SESSIONID') Session_ID FROM DUAL;
To display rows 5 to 7 :
SELECT DEPTNO, ENAME, SAL FROM EMP WHERE ROWID IN (SELECT ROWID FROM EMP WHERE ROWNUM <= 7 MINUS SELECT ROWID FROM EMP WHERE ROWNUM < 5); OR SELECT ename FROM emp GROUP BY ROWNUM, ename HAVING ROWNUM > 1 and ROWNUM < 3;
SELECT COUNT(column_name) FROM user_tab_columns WHERE table_name = 'MYTABLE';
dbms_output.enable(4000); /*allows the output buffer to be increased to the specified number of bytes */ DECLARE BEGIN dbms_output.enable(4000); FOR i IN 1..400 LOOP DBMS_OUTPUT.PUT_LINE(i); END LOOP; END; /
Set the following to some other character. By default, it is &.
set define '~'
Change the Environment Options Like this :
set trimspool on
set timeout on
Difference 1:
Difference 2:
SELECT organization_id,name FROM hr_all_organization_units WHERE organization_id in ( SELECT ORGANIZATION_ID_CHILD FROM PER_ORG_STRUCTURE_ELEMENTS CONNECT BY PRIOR ORGANIZATION_ID_CHILD = ORGANIZATION_ID_PARENT START WITH ORGANIZATION_ID_CHILD = (SELECT organization_id FROM hr_all_organization_units WHERE name = 'EBG Corporate Group'));
SELECT DBMS_RANDOM.VALUE (1,2) FROM DUAL;
SELECT FLOOR((date1-date2)*24*60*60)/3600) || ' HOURS ' || FLOOR((((date1-date2)*24*60*60) - FLOOR(((date1-date2)*24*60*60)/3600)*3600)/60) || ' MINUTES ' || ROUND((((date1-date2)*24*60*60) - FLOOR(((date1-date2)*24*60*60)/3600)*3600 - (FLOOR((((date1-date2)*24*60*60) - FLOOR(((date1-date2)*24*60*60)/3600)*3600)/60)*60))) || ' SECS ' time_difference FROM my_table;
I have this string in a column named location
LOT 8 CONC3 RR
Using instr and substr, I want to take whatever value follows LOT and put it into a different column and whatever value follows CONC and put it into a different column
select substr('LOT 8 CONC3 RR',4,instr('LOT 8 CONC3 RR','CONC')-4) from dual; select substr('LOT 8 CONC3 RR',-(length('LOT 8 CONC3 RR')-(instr('LOT 8 CONC3 RR','CONC')+3))) from dual
select text from all_source where name = 'X' order by line; select text from user_source where name = 'X' select text from user_source where type = 'procedure' and name='procedure_name'; select name,text from dba_source where name='ur_procedure' and owner='scott';
select to_number('-999,999.99', 's999,999.99') from dual; -999,999.99 select to_number('+0,123.45', 's999,999,999.99') from dual; 123.45 select to_number('+999,999.99', 's999,999.99') from dual; 999,999.99
select column_name from user_tab_columns where TABLE_NAME = 'EMP' select column_name from all_tab_columns where TABLE_NAME = 'EMP' select column_name from dba_tab_columns where TABLE_NAME = 'EMP' select column_name from cols where TABLE_NAME = 'EMP'
I have a table have
a,b,c field,
a,b should be unique, and leave max(c) row in.
delete from 'table'
where (a,b,c) not in (select a,b,max(c) from 'table' group by a,b);
declare -- we need one here to get a single quote into the variable v_str varchar2 (20) := 'O''reilly''s'; begin DBMS_OUTPUT.PUT_LINE ( 'original single quoted v_str= ' || v_str ); v_str := replace(v_str, '''', ''''''); DBMS_OUTPUT.PUT_LINE ( 'after double quoted v_str= ' || v_str ); end; SQL> / original single quoted v_str= O'reilly's after double quoted v_str= O''reilly''s
CREATE OR REPLACE FUNCTION to_hms (i_days IN number) RETURN varchar2 IS BEGIN RETURN TO_CHAR (TRUNC (i_days)) || ' days ' || TO_CHAR (TRUNC (SYSDATE) + MOD (i_days, 1), 'HH24:MI:SS'); END to_hms; select to_hms(to_date('17-Jan-2002 13:20:20', 'dd-Mon-yyyy hh24:mi:ss') - to_date('11-Jan-2002 11:05:05', 'dd-Mon-yyyy hh24:mi:ss')) from dual;
The table is both the schemas should have the same structure. The data in it could be the same or different
a-b and b-a
select * from a.a minus select * from b.a and select * from b.a minus select *
from a.a
select * from user_jobs; exec dbms_job.remove(job_no);
Update tblname Set column1 = column2, Column2 = column1;
I have the number e.g. 63,9823874012983 and I want to round it to 63,98 and at the
same time change the, to a.
select round(replace('63,9823874012983',',','.'),2) from dual;
select trunc(sysdate, 'y') from dual; 01-jan-2002 last year this month through a select statement select add_months(sysdate, -12) from dual; 05-APR-01
create sequence sh increment by 1 start with 0;
select next_day(sysdate-7,'SUNDAY'), next_day(sysdate,'SATURDAY') from dual; NEXT_DAY( NEXT_DAY( --------- --------- 07-APR-02 13-APR-02
You liked the article?
Like: 0
Vote for difficulty
Current difficulty (Avg): Medium
TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.